package com.doitedu.doit30.hbase.client;

import com.doitedu.doit30.hbase.utils.HbaseUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Date 22.6.9
 * @Created by HANGGE
 * @Description
 * create table
 *   create  't_java'  , 'cf1'(至少需要一个列族)
 */
public class _01CreateTable {
    public static void main(String[] args) throws Exception {
        Connection conn = HbaseUtil.getHbaseConnection();
        Admin admin = HbaseUtil.getAdmin(conn);


        admin.close();
        conn.close();
    }

    /**
     * 预分region表
     * @param admin
     * @throws IOException
     */
    private static void createPreRegionTable(Admin admin) throws IOException {
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("tb_java_pre"));
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes());
        ColumnFamilyDescriptor cf = columnFamilyDescriptorBuilder.build();
        tableDescriptorBuilder.setColumnFamily(cf) ;
        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();

        // 创建预分region表
        /**
         * 参数一  表的描述器
         * 参数二  分割点   create  'tb_x' , 'cf' , SPLITS=>['f'.getBytes , 'l'.getBytes , 't'.getBytes]
         */
        // 表的分割点
        byte[][] keys = {"f".getBytes() , "l".getBytes() , "t".getBytes()};
        admin.createTable(tableDescriptor , keys);
    }

    /**
     * 建表的时候列族设置属性
     * @param admin
     * @throws IOException
     */
    private static void createTableWithConfiguration(Admin admin) throws IOException {
        // 设置属性  VERSIONS  TTL

        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("tb_java3"));
        // 列族1
        ColumnFamilyDescriptorBuilder cf1Build = ColumnFamilyDescriptorBuilder.newBuilder("cf1".getBytes());
        /*设置属性 */
        cf1Build.setMaxVersions(3) ;
        cf1Build.setTimeToLive(300) ;  // 单位秒
        ColumnFamilyDescriptor cf1 = cf1Build.build();

        // 列族2
        ColumnFamilyDescriptorBuilder cf2Build = ColumnFamilyDescriptorBuilder.newBuilder("cf2".getBytes());
        ColumnFamilyDescriptor cf2 = cf2Build.build();
        // 将多个列族放在集合中
        ArrayList<ColumnFamilyDescriptor> cfs = new ArrayList<>();
        cfs.add(cf1);
        cfs.add(cf2);

        /*  List<ColumnFamilyDescriptor> list = Arrays.asList(cf1, cf2);*/
        // 添加
        tableDescriptorBuilder.setColumnFamilies(cfs) ;

        TableDescriptor td = tableDescriptorBuilder.build();
        admin.createTable(td);
    }

    /**
     * 创建多个列族表
     * @param admin
     * @throws IOException
     */
    private static void createTableMoreCF(Admin admin) throws IOException {
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("tb_java2"));
        // 列族1
        ColumnFamilyDescriptorBuilder cf1Build = ColumnFamilyDescriptorBuilder.newBuilder("cf1".getBytes());
        ColumnFamilyDescriptor cf1 = cf1Build.build();

        // 列族2
        ColumnFamilyDescriptorBuilder cf2Build = ColumnFamilyDescriptorBuilder.newBuilder("cf2".getBytes());
        ColumnFamilyDescriptor cf2 = cf2Build.build();
        // 将多个列族放在集合中
        ArrayList<ColumnFamilyDescriptor> cfs = new ArrayList<>();
        cfs.add(cf1);
        cfs.add(cf2);

        /*  List<ColumnFamilyDescriptor> list = Arrays.asList(cf1, cf2);*/
        // 添加
        tableDescriptorBuilder.setColumnFamilies(cfs) ;

        TableDescriptor td = tableDescriptorBuilder.build();
        admin.createTable(td);
    }


    /**
     * 一个列族的方法
     * @param admin
     * @throws IOException
     */
    private static void createTbaleOneCF(Admin admin) throws IOException {
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("tb_java"));
        // 设置至少一个列族
        /**
         * 列族描述器
         * 列族描述器构建器  构建  列族描述器
         * 设置到表中
         */
      // 在指定的名称空间下建表
      //  TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("doit30:tb_java"));
        // 1)   列族描述器构建器
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("cf1".getBytes());
        // 2)   构建列族描述器
        ColumnFamilyDescriptor cf = columnFamilyDescriptorBuilder.build();
        // 3)   添加到表中
        tableDescriptorBuilder.setColumnFamily(cf) ;
        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
        /**
         * 建表
         *   1 需要参数  表的描述器 (接口)  不能直接new
         *   2 表的描述器  可以使用 表的描述器构建器去构建
         *   TableDescriptorBuilder  --->build --->TableDescriptor
         *
         */
        admin.createTable(tableDescriptor);
    }


}
